package com.ruoyi.admin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.admin.domain.ZhxComment;
import com.ruoyi.admin.domain.ZhxJielong;
import com.ruoyi.admin.domain.ZhxNote;
import com.ruoyi.admin.mapper.ZhxJielongMapper;
import com.ruoyi.admin.mapper.ZhxNoteMapper;
import com.ruoyi.admin.service.IZhxJielongService;
import com.ruoyi.common.core.domain.entity.User;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.admin.mapper.ZhxLikeJielongMapper;
import com.ruoyi.admin.domain.ZhxLikeJielong;
import com.ruoyi.admin.service.IZhxLikeJielongService;

/**
 * 用户点赞接龙Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-11
 */
@Service
public class ZhxLikeJielongServiceImpl implements IZhxLikeJielongService 
{
    @Autowired
    private ZhxLikeJielongMapper zhxLikeJielongMapper;

    @Autowired
    private ZhxJielongMapper zhxJielongMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ZhxNoteMapper zhxNoteMapper;

    /**
     * 查询用户点赞接龙
     * 
     * @param likeJielongId 用户点赞接龙主键
     * @return 用户点赞接龙
     */
    @Override
    public ZhxLikeJielong selectZhxLikeJielongByLikeJielongId(Long likeJielongId)
    {
        return zhxLikeJielongMapper.selectZhxLikeJielongByLikeJielongId(likeJielongId);
    }

    /**
     * 查询用户点赞接龙列表
     * 
     * @param zhxLikeJielong 用户点赞接龙
     * @return 用户点赞接龙
     */
    @Override
    public List<ZhxLikeJielong> selectZhxLikeJielongList(ZhxLikeJielong zhxLikeJielong)
    {
        return zhxLikeJielongMapper.selectZhxLikeJielongList(zhxLikeJielong);
    }

    /**
     * 新增用户点赞接龙
     * 
     * @param zhxLikeJielong 用户点赞接龙
     * @return 结果
     */
    @Override
    public int insertZhxLikeJielong(ZhxLikeJielong zhxLikeJielong)
    {
        zhxLikeJielong.setCreateTime(DateUtils.getNowDate());
        return zhxLikeJielongMapper.insertZhxLikeJielong(zhxLikeJielong);
    }

    /**
     * 新增用户点赞接龙
     * 接龙点赞+1
     * 用户获赞数+1
     * 新增接龙点赞互动消息
     * @param zhxLikeJielong 用户点赞接龙
     * @return 结果
     */
    @Override
    public int insertZhxLikeJielongTodo(ZhxLikeJielong zhxLikeJielong)
    {
        ZhxJielong zhxJielong = zhxJielongMapper.selectZhxJielongByJielongId(zhxLikeJielong.getJielongId());
        // 接龙点赞+1
        zhxJielong.setLikeNum(zhxJielong.getLikeNum() + 1);
        zhxJielongMapper.updateZhxJielong(zhxJielong);
        zhxLikeJielong.setUserId(SecurityUtils.getUserId());
        zhxLikeJielong.setCreateTime(DateUtils.getNowDate());

//        User user = userMapper.selectUserByIdEasy(zhxJielong.getUserId());
        User user = redisCache.getCacheObject("user:" + zhxJielong.getUserId());
        if (user == null) {
            user = userMapper.selectUserByIdEasy(zhxJielong.getUserId());
        }
        // 用户获赞数+1
        user.setGetLikeNum(user.getGetLikeNum() + 1);
        userMapper.updateUserEasy(user);
        redisCache.setCacheObject("user:" + user.getUserId(), user);

        // 新增接龙点赞互动消息
        ZhxNote zhxNote = new ZhxNote();
        // 信息类型（1接龙点赞 2评论点赞 3话题点赞 4评论 5回复）
        zhxNote.setNoteType("1");
        zhxNote.setNoteBodyId(zhxLikeJielong.getJielongId());
        zhxNote.setUserId(zhxLikeJielong.getUserId());
        zhxNote.setToUserId(zhxJielong.getUserId());
        zhxNote.setIsRead("0");
        zhxNoteMapper.insertZhxNote(zhxNote);

        // redis数据新增
        Map<String, Long> zhxLikeJielongCache = new HashMap<>();
        zhxLikeJielongCache = redisCache.getCacheObject("zhxLikeJielongCache");
        zhxLikeJielongCache.put(zhxLikeJielong.getUserId() + "-" + zhxLikeJielong.getJielongId(),1L);
        redisCache.setCacheObject("zhxLikeJielongCache", zhxLikeJielongCache);

        return zhxLikeJielongMapper.insertZhxLikeJielong(zhxLikeJielong);
    }

    /**
     * 修改用户点赞接龙
     * 
     * @param zhxLikeJielong 用户点赞接龙
     * @return 结果
     */
    @Override
    public int updateZhxLikeJielong(ZhxLikeJielong zhxLikeJielong)
    {
        return zhxLikeJielongMapper.updateZhxLikeJielong(zhxLikeJielong);
    }

    /**
     * 批量删除用户点赞接龙
     * 
     * @param likeJielongIds 需要删除的用户点赞接龙主键
     * @return 结果
     */
    @Override
    public int deleteZhxLikeJielongByLikeJielongIds(Long[] likeJielongIds)
    {
        return zhxLikeJielongMapper.deleteZhxLikeJielongByLikeJielongIds(likeJielongIds);
    }

    /**
     * 删除用户点赞接龙信息
     * 
     * @param likeJielongId 用户点赞接龙主键
     * @return 结果
     */
    @Override
    public int deleteZhxLikeJielongByLikeJielongId(Long likeJielongId)
    {
        return zhxLikeJielongMapper.deleteZhxLikeJielongByLikeJielongId(likeJielongId);
    }

    /**
     * 删除用户点赞接龙信息
     * 接龙点赞-1
     * 用户获赞数-1
     * @param likeJielongId 用户点赞接龙主键
     * @return 结果
     */
    @Override
    public int deleteZhxLikeJielongByLikeJielongIdTodo(Long likeJielongId)
    {
        ZhxLikeJielong zhxLikeJielong = zhxLikeJielongMapper.selectZhxLikeJielongByLikeJielongId(likeJielongId);
        ZhxJielong zhxJielong = zhxJielongMapper.selectZhxJielongByJielongId(zhxLikeJielong.getJielongId());
        // 接龙点赞+1
        zhxJielong.setLikeNum(zhxJielong.getLikeNum() - 1);
        zhxJielongMapper.updateZhxJielong(zhxJielong);

        User user = redisCache.getCacheObject("user:" + zhxJielong.getUserId());
        if (user == null) {
            user = userMapper.selectUserByIdEasy(zhxJielong.getUserId());
        }
        // 用户获赞数-1
        user.setGetLikeNum(user.getGetLikeNum() - 1);
        userMapper.updateUserEasy(user);
        redisCache.setCacheObject("user:" + user.getUserId(), user);

        // 删除接龙点赞互动消息
        ZhxNote zhxNote = new ZhxNote();
        // 信息类型（1接龙点赞 2评论点赞 3话题点赞 4评论 5回复）
        zhxNote.setNoteType("1");
        zhxNote.setNoteBodyId(zhxLikeJielong.getJielongId());
        zhxNote.setUserId(zhxLikeJielong.getUserId());
        zhxNoteMapper.deleteZhxNoteByNoteInfo(zhxNote);

        // redis数据删除
        Map<String, Long> zhxLikeJielongCache = new HashMap<>();
        zhxLikeJielongCache = redisCache.getCacheObject("zhxLikeJielongCache");
        zhxLikeJielongCache.remove(zhxLikeJielong.getUserId() + "-" + zhxLikeJielong.getJielongId(),1L);
        redisCache.setCacheObject("zhxLikeJielongCache", zhxLikeJielongCache);

        return zhxLikeJielongMapper.deleteZhxLikeJielongByLikeJielongId(likeJielongId);
    }
}
